package neptune.manager.connector;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.libvirt.Connect;
import org.libvirt.ConnectAuth;
import org.libvirt.LibvirtException;

public final class NeptuneVirConnectAuth extends ConnectAuth {

	{
		credType = new CredentialType[] { CredentialType.VIR_CRED_AUTHNAME,
				CredentialType.VIR_CRED_ECHOPROMPT,
				CredentialType.VIR_CRED_REALM,
				CredentialType.VIR_CRED_PASSPHRASE,
				CredentialType.VIR_CRED_NOECHOPROMPT };
	}

	@Override
	public int callback(Credential[] cred) {
		System.out.println("CHIAMATO callback");
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		try {
			for (Credential c : cred) {
				String response = "";
				switch (c.type) {
				case VIR_CRED_USERNAME:
					System.out.println("VIR_CRED_USERNAME");
				case VIR_CRED_AUTHNAME:
					System.out.println("VIR_CRED_AUTHNAME");
				case VIR_CRED_ECHOPROMPT:
					System.out.println("VIR_CRED_ECHOPROMPT");
				case VIR_CRED_REALM:
					System.out.println(c.prompt);
					response = in.readLine();
					break;
				case VIR_CRED_PASSPHRASE:
					System.out.println("VIR_CRED_PASSPHRASE");
				case VIR_CRED_NOECHOPROMPT:
					System.out.println(c.prompt);
					System.out
							.println("WARNING: THE ENTERED PASSWORD WILL NOT BE MASKED!");
					response = in.readLine();
					break;
				default:
					System.out.println("DEFAULT");
					break;
				}
				if (response.equals("") && !c.defresult.equals("")) {
					c.result = c.defresult;
				} else {
					c.result = response;
				}
				if (c.result.equals("")) {
					return -1;
				}
			}
		} catch (Exception e) {
			return -1;
		}
		return 0;
	}

	public static void main(String[] args) throws LibvirtException {
		NeptuneVirConnectAuth auth = new NeptuneVirConnectAuth();
		new Connect("xen+ssh://root@127.0.0.1/", auth, 1);
	}

}
